<template>
  <div>
    <!-- 新增或者修改 -->
    <el-dialog
    :visible.sync="visible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @closed="closedDialog"
    :append-to-body="true"
    :title="id ? $t('SYS.edit'): $t('SYS.add')">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="submitForm" :label-width="$i18n.locale=='zh_cn'? '120px':'160px'">
      <el-form-item prop="beanName" :label="$t('schedule.beanName')">
        <el-input v-model="dataForm.beanName" :placeholder="$t('schedule.beanNameTips')"></el-input>
      </el-form-item>
      <el-form-item prop="params" :label="$t('schedule.params')">
        <el-input v-model="dataForm.params" :placeholder="$t('schedule.params')"></el-input>
      </el-form-item>
      <el-form-item prop="cronExpression" :label="$t('schedule.cronExpression')">
        <el-input v-model="dataForm.cronExpression" :placeholder="$t('schedule.cronExpressionTips')"></el-input>
      </el-form-item>
      <el-form-item prop="remark"  :label="$t('schedule.remark')">
        <el-input v-model="dataForm.remark" :placeholder="$t('SYS.pleaseInput')+$t('schedule.remark')"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{$t('SYS.close')}}</el-button>
      <el-button type="primary" @click="submitForm" :disabled="isSubmit">{{$t('SYS.check')}}</el-button>
    </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props:{
    id:{
      type:[String,Number],
      default:()=>{return ""} 
    }
  },
  data(){
    return{
      isSubmit:false,
      visible:false,
      dataRule:{
        beanName: [
          { required: true, message: this.$t('SYS.pleaseInput')+this.$t('schedule.beanName'), trigger: 'blur' }
        ],
        cronExpression: [
          { required: true, message: this.$t('SYS.pleaseInput')+this.$t('schedule.cronExpression')+' '+this.$t('schedule.cronExpressionTips'), trigger: 'blur' }
        ]
      },
      dataForm:{
        id:"",
        beanName:"",
        params:"",
        cronExpression:"",
        remark:""
      }
    }
  },
  methods:{
    init(){
      this.visible = true
    },
    submitForm(){
      this.$refs['dataForm'].validate((valid)=>{
        if(!valid){
          return false
        }
        this.isSubmit = true
        this.$http[!this.dataForm.id ? 'post' : 'put'](this.$baseUrl.submitSysSchedule, this.dataForm).then(({data:res})=>{
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message.success(this.dataForm.id?this.$t("request.editSuccess"):this.$t("request.addSuccess"))
          this.visible  = false
          this.$emit('hasSubmit')
        })
      })
    },
    getData(){
      this.$http.get(this.$baseUrl.sysSchedule+'/' + this.id).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    closedDialog(){
      this.$emit('closedDialog')
    }
  },
  mounted(){
    if(this.id){
      this.getData()
    }
  }
}
</script>

<style>

</style>